﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Windows.Forms;

namespace PMC.Properties {
    
    
    // This class allows you to handle specific events on the settings class:
    //  The SettingChanging event is raised before a setting's value is changed.
    //  The PropertyChanged event is raised after a setting's value is changed.
    //  The SettingsLoaded event is raised after the setting values are loaded.
    //  The SettingsSaving event is raised before the setting values are saved.
    public sealed partial class Settings
    {
        private Stack<SettingChangingEventArgs> settingChangingStack = new Stack<SettingChangingEventArgs>();

        public bool SettingsChanged
        {
            get { return settingChangingStack.Count > 0; }
        }

        public int NumChanges
        {
            get { return settingChangingStack.Count; }
        }
        
        public Settings()
        {
            // // To add event handlers for saving and changing settings, uncomment the lines below:
            //
            this.SettingChanging += this.SettingChangingEventHandler;
            //
            this.SettingsSaving += this.SettingsSavingEventHandler;
            //
            this.SettingsReloaded += new SettingsReloadedEventHandler(Settings_SettingsReloaded);
        }

        void Settings_SettingsReloaded(object sender, EventArgs args)
        {
            settingChangingStack = new Stack<SettingChangingEventArgs>();
        }

        public new void Reload()
        {
            base.Reload();
            Settings_SettingsReloaded(this, EventArgs.Empty);
        }

        public event SettingsReloadedEventHandler SettingsReloaded;

        private void SettingChangingEventHandler(object sender, SettingChangingEventArgs e)
        {
            settingChangingStack.Push(e);
        }
        
        private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e)
        {
            settingChangingStack = new Stack<SettingChangingEventArgs>();
        }


        public void Undo()
        {
            var setting = settingChangingStack.Pop(); //Maybe settings.Cancel = true; will work but I doupt it
            this[setting.SettingName] = setting.SettingKey;
        }
    }

    public delegate void SettingsReloadedEventHandler(object sender, EventArgs args);
}
